<template>
  <div class="playController">
    <div class="left" @click="getshow">
      <img :src="playlist[playCurrentIndex].al.picUrl" alt="" />
      <div class="content">
        <div class="title">{{ playlist[playCurrentIndex].al.name }}</div>
        <div class="tips">横滑可以上下切换</div>
      </div>
    </div>
    <div class="right">
      <svg
        v-if="playFlag"
        class="icon zanting"
        aria-hidden="true"
        @click="play"
      >
        <use xlink:href="#icon-bofang1"></use>
      </svg>
      <svg
        v-if="!playFlag"
        class="icon zanting"
        aria-hidden="true"
        @click="play"
      >
        <use xlink:href="#icon-bofang"></use>
      </svg>
      <svg class="icon liebiao" aria-hidden="true">
        <use xlink:href="#icon-24gl-playlistMusic2"></use>
      </svg>
    </div>
    <play-music
      @back="show = false"
      v-if="show"
      :playDetail="playlist[playCurrentIndex]"
      :playFlag="playFlag"
      :play="play"
    ></play-music>
    <audio
      ref="audios"
      :src="`https://music.163.com/song/media/outer/url?id=${playlist[playCurrentIndex].id}.mp3`"
    ></audio>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import playMusic from "@/components/playMusic.vue";
import emitter from "@/util/bus";
export default {
  components: { playMusic },
  computed: {
    ...mapState(["playlist", "playCurrentIndex", "lyric"]),
  },
  data() {
    return {
      playFlag: true,
      show: false,
    };
  },
  mounted() {
    this.setCurAudio(this.$refs.audios)
    emitter.on("updatePlay", (res) => {
      let { playFlag } = res;
      this.playFlag = playFlag;
      let vm = this;
      setTimeout(() => {        
        this.$refs.audios.play();
        vm.$store.state.timer = setInterval(() => {
          vm.$store.commit("setCurrentTime", vm.$refs.audios.currentTime);
        }, 200);
      }, 200);
      console.log(res, 777, this.$refs.audios.paused);
    });
  },
  methods: {
    ...mapMutations(["setCurAudio"]),
    ...mapActions(["reqLyric"]),
    play() {
      if (this.$refs.audios.paused) {
        this.playFlag = false;
        this.$refs.audios.play();
        console.log(4444);
        this.reqLyric(this.playlist[this.playCurrentIndex]);
        this.updateTime();
      } else {
        this.playFlag = true;
        this.$refs.audios.pause();
        this.reqLyric(this.playlist[this.playCurrentIndex]);
        clearInterval(this.$store.state.timer);
      }
    },
    getshow() {
      this.show = !this.show;
      if (this.lyric == "") {
        this.reqLyric(this.playlist[this.playCurrentIndex]);
      }
    },
    updateTime() {
      let vm = this;
      vm.$store.state.timer = setInterval(() => {
        vm.$store.commit("setCurrentTime", vm.$refs.audios.currentTime);
      }, 200);
    },
  },
};
</script>

<style lang="less" scoped>
.playController {
  width: 7.5rem;
  height: 1.2rem;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #ccc;
  background-color: #fff;
  .left {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 0 0.4rem;
    width: 5rem;
    height: 1.2rem;
    img {
      width: 0.8rem;
      height: 0.8rem;
      margin-right: 10px;
      border-radius: 0.4rem;
    }
    .content {
      width: 4rem;
      padding-left: 0.2rem;
      .title {
        width: 100%;
        height: 0.4rem;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .tips {
        width: 70%;
        font-size: 0.2rem;
        color: #999;
      }
    }
  }
  .right {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .icon {
      margin: 0 0.2rem;
      width: 0.5rem;
      height: 0.5rem;
    }
  }
}
</style>
